Deep Learning to find Orchards in Annapolis Royal

In [21]:
#######################################################################################################################
# Script Name: Orchards_Detection.ipynb
# Purpose: To complete the entire object detection workflow in one script
# Modifications to code and comments by: Shannon England
#
# Note: the original script was obtained from the arcgis-python-api-master folder,downloaded from the website
# https://github.com/Esri/arcgis-python-api
#######################################################################################################################
In [1]:
mosaic = r'E:\ShannonE\GDA_Project_SE\Historical_Orthomosaic\Rotated_Mosaic.tif' 
mosaic
Out[1]:
'E:\\ShannonE\\GDA_Project_SE\\Historical_Orthomosaic\\Rotated_Mosaic.tif'
In [2]:
training_data = r'E:\ShannonE\GDA_Project_SE\Historical_Orthomosaic\Attempt7'
training_data
Out[2]:
'E:\\ShannonE\\GDA_Project_SE\\Historical_Orthomosaic\\Attempt7'

Prepare Data

In [3]:
# importing prepare data library from arcgis to apply transformations and enhance the orchards layer
from arcgis.learn import prepare_data

data = prepare_data(training_data, {1: 'Young Orchard',
                                    2: 'Mixed Orchard',
                                    4: 'Overexposed'})
In [4]:
data.show_batch()

Train SingleShotDetector Model

In [16]:
# importing the object detection model that will be used
from arcgis.learn import SingleShotDetector

# settings for single shot detector
ssd = SingleShotDetector(data, grids=[3,2,1])
In [ ]:
# finding the optimum learning rate - low learning rate leads to slow training of model
ssd.lr_find()
In [17]:
# showing the model learning the task by showing the loss and validation for the training data
ssd.fit(10, slice(0.001, 0.02))
epoch train_loss valid_loss
1 624.910278 1663.265381
2 366.284698 268.460968
3 271.249817 121.763870
4 223.198349 120.166855
5 196.226822 107.433594
6 174.164185 102.953674
7 161.324509 112.837585
8 147.281372 116.220146
9 138.099396 101.555275
10 129.157043 95.537743
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpro_clone\lib\site-packages\torch\nn\_reduction.py:49: UserWarning: size_average and reduce args will be deprecated, please use reduction='sum' instead.
  warnings.warn(warning.format(ret))
In [19]:
# visualizing the results of the trained model
ssd.show_results(rows=20, thresh=0.1)
In [12]:
# saving the trained model
ssd.save('OrchardDetector7_2')
Created model files at E:\ShannonE\GDA_Project_SE\Historical_Orthomosaic\Attempt7\models\OrchardDetector7_2